﻿using CommonTools;
using Sunny.UI;
using System;
using UserData;

namespace StationDemo
{
    public class LoadSeverMgr
    {
        private static LoadSeverMgr instance = null;

        private static readonly Object _lock = new object();

        public static LoadSeverMgr Instance
        {
            get
            {
                if (instance == null)
                {
                    lock (_lock)
                    {
                        instance = new LoadSeverMgr();
                    }
                }
                return instance;
            }
        }


        public LoadServer server1 = new LoadServer("治具1");
        public LoadServer server2 = new LoadServer("治具2");
        private string name = "Server";
       
        public void Init()
        {
            server1.Run("127.0.0.1", 8000);
            server2.Run("127.0.0.1", 8005);
        }

        public void Start()
        {
            if (Param_System.Instance.启用A工位._strParamVal)
            {
                Function_通知标定开始测试(server1);
            }
            if (Param_System.Instance.启用B工位._strParamVal)
            {
                Function_通知标定开始测试(server2);
            }


        }


        public void Stop()
        {
            if (Param_System.Instance.启用A工位._strParamVal)
            {
                Function_通知标定停止测试(server1);
            }
            if (Param_System.Instance.启用B工位._strParamVal)
            {
                Function_通知标定停止测试(server2);
            }

        }

        public void Functio_停止取图()
        {
            string cmd = $"{(int)ServerCmd.停止取图}";
            if (Param_System.Instance.启用A工位._strParamVal)
            {
                server1.Cmd_运控平台通知标定平台测试(cmd);
            }
            if (Param_System.Instance.启用B工位._strParamVal)
            {
                server2.Cmd_运控平台通知标定平台测试(cmd);
            }

        }


        public void Function_通知标定开始测试(LoadServer loadServer)
        {
            int re = 0;
            if (loadServer.Function_IsConnect())
            {
                re = loadServer.Cmd_运控平台通知标定平台启动();
                if (re == 0)
                {
                    LogHelper.OnTest_ShowLog(name, $"{loadServer.name}启动成功");

                }
                else
                {
                    WaranResult result = AlarmMgr.GetIntance().WarnWithDlg((int)AlarmList.通讯超时, new string[] { "确定" }, new string[] { $"{loadServer.name}通讯超时，请检查通讯是否连接" });
                }

            }
        }


        public void Function_通知标定停止测试(LoadServer loadServer)
        {
            int re = 0;
            if (loadServer.Function_IsConnect())
            {
                re = loadServer.Cmd_运控平台通知标定平台停止();
                if (re == 0)
                {
                    LogHelper.OnTest_ShowLog(name, $"{loadServer.name}停止成功");

                }
                else
                {
                    WaranResult result = AlarmMgr.GetIntance().WarnWithDlg((int)AlarmList.通讯超时, new string[] { "确定" }, new string[] { $"{loadServer.name}通讯超时，请检查通讯是否连接" });
                }

            }

        }

     
        public bool Function_清空标定数据(LoadServer loadServer)
        {
            int res = 0;
            string cmd = $"{(int)ServerCmd.清标定数据}";
        retry:
            res = loadServer.Cmd_运控平台通知标定平台测试(cmd);
            if (res == -2)
            {
                WaranResult waranResult = AlarmMgr.GetIntance().WarnWithDlg((int)AlarmList.通讯超时, new string[] { "确定" }, new string[] { $"{loadServer.name}通讯超时，请检查通讯是否连接" });
                if (waranResult == WaranResult.Custom1)
                {
                    goto retry;
                }
                else
                {

                    return false;
                }
            }
            if (res != 0)
            {

                return false;
            }
            return true;

        }
        public bool Function_取图(LoadServer loadServer,int index)
        {
            int res = 0;
            string cmd = $"{(int)ServerCmd.取图},{index}";
        retry:
            res = loadServer.Cmd_运控平台通知标定平台测试(cmd);
            if (res == -2)
            {
                WaranResult waranResult = AlarmMgr.GetIntance().WarnWithDlg((int)AlarmList.取图超时, new string[] { "重试", "NG" }, new string[] { "取图超时超时，请选择重新取图或者取图NG" });
                if (waranResult == WaranResult.Custom1)
                {
                    goto retry;
                }
                else
                {

                    return false;
                }
            }
            if (res != 0)
            {
               
                return false;
            }
            return true;

        }

        public bool Function_点亮(LoadServer loadServer,string stringSN, ref double voltage, ref double current)
        {
            string cmd = $"{(int)ServerCmd.点亮},{stringSN}";
          
            int res = 0;
            string result = "";
        retry:
            res = loadServer.Cmd_运控平台通知标定平台测试并返回结果(cmd, ref result, Param_System.Instance.点亮通讯超时时间._strParamVal);

            if (res == -2)
            {
                WaranResult waranResult = AlarmMgr.GetIntance().WarnWithDlg((int)AlarmList.点亮超时, new string[] { "重试", "点亮失败，换料" }, new string[] { "点亮超时，请选择重新点亮或者换料测试" });
                if (waranResult == WaranResult.Custom1)
                {
                    goto retry;
                }
                else
                {
                    return false;
                }

            }
            else if (res != 0)
            {
               
                return false;
            }
            string[] str2 = result?.Split(',');
            if (str2.Length >= 2)
            {
                voltage = str2[0].ToDouble();
                current = str2[1].ToDouble();
            }
          
            return true;

        }
        public bool Function_对心(LoadServer loadServer, ref double Cx, ref double Cy)
        {
            string cmd = $"{(int)ServerCmd.对心}";

            int res = 0;
            string result = "";
        retry:
            res = loadServer.Cmd_运控平台通知标定平台测试并返回结果(cmd, ref result, Param_System.Instance.计算图像中心超时时间._strParamVal);


            if (res == -2)
            {
                WaranResult waranResult = AlarmMgr.GetIntance().WarnWithDlg((int)AlarmList.图像中心计算超时, new string[] { "重试", "NG" }, new string[] { "图像中心计算超时，请重试或选择NG" });
                if (waranResult == WaranResult.Custom1)
                {
                    goto retry;
                }
                else
                {
                    
                    return false;
                }

            }
            else if (res != 0)
            {
               
                return false;
            }
            string[] str2 = result.Split(',');
            if (str2.Length >= 2)
            {
                Cx = str2[0].ToDouble();
                Cy = str2[1].ToDouble();
            }
            else
            {
                return false;
            }
            return true;

        }

        public bool Function_标定计算(LoadServer loadServer,out double[] values)
        {
            string cmd = $"{(int)ServerCmd.测试}";

            int res = 0;
            string result = "";
        retry:
            res = loadServer.Cmd_运控平台通知标定平台测试并返回结果(cmd,ref result,Param_System.Instance.标定计算通讯超时时间._strParamVal);


            if (res == -2)
            {
                WaranResult waranResult = AlarmMgr.GetIntance().WarnWithDlg((int)AlarmList.标定计算超时, new string[] { "重试", "点亮失败，换料" }, new string[] { "点亮超时，请选择重新点亮或者换料测试" });
                if (waranResult == WaranResult.Custom1)
                {
                    goto retry;
                }
                else
                {
                    values = null;
                    return false;
                }

            }
            else if (res != 0)
            {
                values = null;
                return false;
            }
            string[] str2 = result.Split(',');
            values = new double[str2.Length];
            for (int i = 0;i < str2.Length;i++)
            {
                values[i] = Convert.ToDouble(str2[i]);


            }


            return true;

        }
       
    }
}
